﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlogManager.Core.BaseArchitecture;

namespace BlogManager.Application
{
    public partial class Post : AppFormBase
    {
        private int? postId = null;
        private int? siteId = null;

        public override void PageLoad()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["postId"]))
                postId = Convert.ToInt32(Request.QueryString["postId"]);

            if (!string.IsNullOrEmpty(Request.QueryString["siteId"]))
                siteId = Convert.ToInt32(Request.QueryString["siteId"]);
        }

        public override void PageFirstTimeLoad()
        {
            TextBoxForDate.Text = DateTime.Now.ToShortDateString();
        }

        public override void PageDataLoad()
        {
            if (postId != null)
            {
                var blogPost = Data.Entities.Blog_Post.SingleOrDefault(x=>x.Id == postId.Value);

                TextBoxForTitle.Text = blogPost.Title;
                TextBoxForDate.Text = blogPost.PublicationDate.ToShortDateString();
                CKEditorForContent.Text = blogPost.Content;

                var relatedCategories = Data.Entities.Blog_Categories_Post.Find(x => x.BlogPostId == postId).Select(x=>x.BlogCategoryId).ToList();
                CategoryControl.SelectedCategories = relatedCategories;

                var relatedTags = Data.Entities.Blog_Tags_Post.Find(x => x.BlogPostId == blogPost.Id).Select(x=>x.BlogTagId).ToList();
                TagControl.SelectedTags = relatedTags;
            }
        }

		protected void ButtonForSaving_Click(object sender, EventArgs e)
		{
            Save();
		}
        protected void ButtonForSavingAndReturning_Click(object sender, EventArgs e)
		{
            Save();
            MessagesForActions.ReturnToCallingPage();
		}

        public override void Save()
        {
            //If we have a postId then we load it.
            var blogPost = (postId == null) ? new Data.Entities.Blog_Post() : Data.Entities.Blog_Post.SingleOrDefault(x => x.Id == postId);

            blogPost.SiteId = siteId.Value;
            blogPost.Title = TextBoxForTitle.Text;
            blogPost.PublicationDate = Convert.ToDateTime(TextBoxForDate.Text);
            blogPost.Content = CKEditorForContent.Text;
            blogPost.UserId = 1;

            try
            {
                blogPost.Save();

                #region ForRelatedCategories
                //Clear related data...
                Data.Entities.Blog_Categories_Post.Delete(x=>x.BlogPostId==blogPost.Id);

                //Insert new realtions...
                var categories = CategoryControl.SelectedCategories;
                foreach (var category in categories)
                {
                    var blogPostCategories = new Data.Entities.Blog_Categories_Post
                                                 {
                                                     BlogPostId = blogPost.Id,
                                                     BlogCategoryId = category
                                                 };
                    blogPostCategories.Save();
                }
                #endregion

                #region ForRelatedTags
                //Clear related data...
                Data.Entities.Blog_Tags_Post.Delete(x => x.BlogPostId == blogPost.Id);

                //Insert new realtions...
                var tags = TagControl.SelectedTags;
                foreach (var tag in tags)
                {
                    var blogPostTags = new Data.Entities.Blog_Tags_Post
                    {
                        BlogPostId = blogPost.Id,
                        BlogTagId = tag
                    };
                    blogPostTags.Save();
                }
                #endregion


                MessagesForActions.Show = true;
                MessagesForActions.Message = "Saved!";
            }
            catch (Exception ex)
            {
                MessagesForActions.Show = true;
                MessagesForActions.Message = "Error!";
            }
        }

        public override void Delete()
        {
            Data.Entities.Blog_Post.Delete(x=>x.Id==postId);
        }
    }
}